package com.aozhi.aipower.operation.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.org.atool.fluent.mybatis.model.StdPagedList;
import com.aozhi.aipower.operation.base.group.Insert;
import com.aozhi.aipower.operation.controller.req.BannerReqForm;
import com.aozhi.aipower.operation.controller.req.BannerReqQuery;
import com.aozhi.aipower.operation.controller.resp.BannerResp;
import com.aozhi.aipower.operation.dao.base.BannerBaseDao;
import com.aozhi.aipower.operation.dao.impl.BannerServiceImpl;
import com.aozhi.aipower.operation.entity.BannerEntity;
import com.aozhi.aipower.operation.mapper.BannerMapper;
import com.aozhi.aipower.operation.wrapper.BannerQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 轮播图控制器
 * @author lunzi
 */
@RestController
@RequestMapping("/banners")
public class BannerController {

    @Autowired
    private BannerServiceImpl bannerDao;

    /**
     * 根据查询条件分页查询轮播图
     * @param reqQuery  查询条件
     * @return  轮播图分页数据
     */
    @GetMapping
    public StdPagedList<BannerResp> list(BannerReqQuery reqQuery) {
        BannerQuery query = BannerQuery.query()
            .where
                .name().like(reqQuery.getName(), StrUtil::isNotBlank).and
                .display().eq(reqQuery.getDisplay(), Objects::nonNull).end()
            .orderBy
                .sort().asc().createdDate().desc().end();
        return bannerDao.stdPagedPoJo(BannerResp.class, query);
    }

    /**
     * 根据 id 查看轮播图信息
     * @param id    轮播图id
     * @return  轮播图信息
     */
    @GetMapping("/{id}")
    public BannerResp get(@PathVariable("id") Integer id) {
        BannerEntity bannerEntity = bannerDao.selectById(id);
        return BeanUtil.copyProperties(bannerEntity, BannerResp.class);
    }

//     @PostMapping
//     public BannerResp save(@RequestBody @Validated(Insert.class) BannerReqForm reqForm) {
//         BannerEntity bannerEntity = BeanUtil.copyProperties(reqForm, BannerEntity.class);
//         bannerDao.save(bannerEntity);
//     }
}
